package cn.umakr.dao.Impl;

import cn.umakr.dao.ManagerDao;
import cn.umakr.entity.Manager;
import cn.umakr.entity.User;
import cn.umakr.util.DbHelper;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangbo on 2017/7/17.
 */
public class ManagerDaoImpl implements ManagerDao {
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * 增加一个Manager对象
     *
     * @param manager
     */
    @Override
    public Integer save(Manager manager) {
        Integer result = 0;
        try {
            String sql = "insert into tab_manager values(null,?,?)";
            Connection connection = DbHelper.getConn();
            result = queryRunner.update(connection, sql, manager.getAname(), manager.getApasswd());
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除一个Manager对象,根据主键删除
     *
     * @param id
     */
    @Override
    public Integer delete(Integer id) {
        Integer result = 0;
        try {
            String sql = "delete from tab_manager where id = ?";
            Connection connection = DbHelper.getConn();
            result = queryRunner.update(connection, sql, id);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改Manager对象
     *
     * @param manager
     */
    @Override
    public Integer update(Manager manager) {
        Integer result = 0;
        try {
            String sql = "update tab_manager set aname = ?,apasswd = ? where id = ?";
            Connection connection = DbHelper.getConn();
            result = queryRunner.update(connection, sql, manager.getAname(), manager.getApasswd(), manager.getId());
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询所有Manager对象
     */
    @Override
    public List<Manager> findAll() {

        List<Manager> managers = new ArrayList<Manager>();
        try {
            String sql = "select * from tab_manager";
            Connection connection = DbHelper.getConn();
            BeanListHandler<Manager> rsh = new BeanListHandler<Manager>(Manager.class);
            managers = queryRunner.query(connection, sql, rsh);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return managers;
    }

    /**
     * 根据主键查找Manager对象
     *
     * @param id
     */
    @Override
    public Manager findById(Integer id) {
        Manager manager = null;
        try {
            String sql = "select * from tab_manager where id = ?";
            Connection connection = DbHelper.getConn();
            BeanHandler<Manager> rsh = new BeanHandler<Manager>(Manager.class);
            manager = queryRunner.query(connection, sql, rsh, id);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return manager;
    }

    /**
     * 根据属性查找Manager对象
     *
     * @param propName
     * @param propValue
     */
    @Override
    public List<Manager> findByProperty(String propName, Object propValue) {
        List<Manager> managers = new ArrayList<Manager>();
        try {
            String sql = "select * from tab_manager where " + propName + "=?";
            Connection connection = DbHelper.getConn();
            BeanListHandler<Manager> rsh = new BeanListHandler<Manager>(Manager.class);
            managers = queryRunner.query(connection,sql,rsh,propValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return managers;
    }
}
